package com.meevle.error
{
	import com.meevle.lang.Messages;
	import com.meevle.model.ViewModelLocator;
	
	import mx.messaging.events.ChannelFaultEvent;
	import mx.rpc.events.FaultEvent;
	
	public class ErrorHandler
	{
		private static var instance:ErrorHandler;
		
		public function ErrorHandler(enforcer:SingletonEnforcer) {
		if (enforcer == null) {
				throw new Error( "You Can Only Have One ErroHandler" );
			}
		}
		
		public static function getInstance() : ErrorHandler {
			if (instance == null) {
				instance = new ErrorHandler( new SingletonEnforcer );
			}
			return instance;
		}
		
		public function handleError(event:FaultEvent) : void {
			
			var errorMessage:String;
			var error:Error;
			if(event.fault)
			{
				errorMessage = event.fault.faultDetail;
			}
			else
			{
				errorMessage = Messages.API_CONNECTION_ERROR;
			}
			
			ViewModelLocator.getInstance().error = new Error(errorMessage);
		}

	}
}

// Utility Class to Deny Access to Constructor
class SingletonEnforcer {}